package creational.abstractfactory;

import com.alibaba.fastjson.JSONObject;
import creational.abstractfactory.product.Computer;
import creational.abstractfactory.product.Keyboard;
import creational.utils.SystemInfoUtils;

import java.net.UnknownHostException;

public class AbstractFactoryDemo {
    public static BrandFactory factory = init();

    public static void main(String[] args) {
        Computer computer = factory.createComputer();
        Keyboard keyboard = factory.createKeyboard();
        keyboard.scanner(computer);
    }

    public static BrandFactory init(){
        try {
            JSONObject sysInfo = SystemInfoUtils.getSysInfo();
            String osName = (String) sysInfo.get("osName");
            if ("Mac OS X".equals(osName)) {
                return new AppleFactory();
            }
            if ("Windows 10".equals(osName)) {
                return new HuaweiFactory();
            }
        } catch (UnknownHostException e) {
            e.printStackTrace();
        }
        return null;
    }
}
